package com.iring.domain.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.iring.domain.config.prop.AliPayProperties;
import com.iring.domain.config.prop.PayPalProperties;
import com.paypal.base.rest.APIContext;
import com.paypal.base.rest.PayPalRESTException;

/**
 * paypal配置
 *
 * @author wanggan
 */
@Configuration
@ConditionalOnClass(PayPalProperties.class)
public class PayConfig {
    public static final String ORDER_CREATED = "CREATED";
    public static final String ORDER_APPROVED = "APPROVED";
    public static final String ORDER_FAILED = "FAILED";
    public static final String ORDER_COMPLETED = "FAILED";
    public static final String WAIT_BUYER_PAY = "WAIT_BUYER_PAY";
    public static final String TRADE_CLOSED = "TRADE_CLOSED";
    public static final String PAYPAL = "paypal";
    public static final String ALIPAY = "alipay";
    public static final String WAIT_USER_PAY = "WAIT_USER_PAY";
    public static final String SYSTEM_PROCESSING = "SYSTEM_PROCESSING";

    @Autowired
    PayPalProperties payPalConfig;

    @Autowired
    AliPayProperties aliPayProperties;

    @Bean
    public APIContext apiContext() throws PayPalRESTException {
        APIContext apiContext = new APIContext(payPalConfig.getClientId(),
                payPalConfig.getClientSecret(), payPalConfig.getMode());
        return apiContext;
    }


    @Bean
    public AlipayClient alipayClient(){
        String url = aliPayProperties.getPayUrl();
        String appid = aliPayProperties.getAppid();
        String appPrivateKey = aliPayProperties.getAppPrivateKey();
        String format = aliPayProperties.getFormat();
        String charset = "UTF-8";
        String aliPubKey = aliPayProperties.getAlipayPubKey();
        String singType = aliPayProperties.getSignType();
        return new DefaultAlipayClient(url, appid, appPrivateKey, format, charset, aliPubKey, singType);
    }


}
